package com.cloudsigns.factorymanager2.produce.activity.produce_team;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.cloudsigns.factorymanager2.BaseActivity;
import com.cloudsigns.factorymanager2.R;
import com.cloudsigns.factorymanager2.produce.adapter.produce_team.ProduceTeamOrderListAdapter;
import com.cloudsigns.factorymanager2.produce.bean.WorkPartOrderResponse;
import com.cloudsigns.factorymanager2.produce.iview.produce_team.IGetProductWorkPartAssignDoneOrderListView;
import com.cloudsigns.factorymanager2.produce.presenter.produce_team.GetProductWorkPartAssignDoneOrderListPresenter;
import com.cloudsigns.factorymanager2.project.utils.ConstantUtil;

import java.util.ArrayList;
import java.util.List;

public class ProduceTeamOrderListActivity extends BaseActivity implements IGetProductWorkPartAssignDoneOrderListView {
    private String TAG = ProduceTeamOrderListActivity.class.getSimpleName();
    private RelativeLayout rl_back;
    private TextView tv_title;
    private View rl_progressbar;
    private ListView listView;

    private GetProductWorkPartAssignDoneOrderListPresenter getProductWorkPartAssignDoneOrderListPresenter;

    private ProduceTeamOrderListAdapter adapter;
    private List<WorkPartOrderResponse> workPartOrderResponseList = new ArrayList<>();

    @Override
    protected void onBaseCreate(Bundle savedInstanceState) {
        setContentView(R.layout.layout_produce_team_order_list);
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        rl_back = findViewById(R.id.rl_back);
        tv_title = findViewById(R.id.tv_title);
        rl_progressbar = findViewById(R.id.rl_progressbar);
        listView = findViewById(R.id.lv);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        tv_title.setText(getResources().getString(R.string.work_sheets));

        getProductWorkPartAssignDoneOrderListPresenter = new GetProductWorkPartAssignDoneOrderListPresenter(this);
        getProductWorkPartAssignDoneOrderListPresenter.getProductWorkPartAssignDoneOrderList();

        adapter = new ProduceTeamOrderListAdapter(this);
        adapter.setData(workPartOrderResponseList);
        listView.setAdapter(adapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                WorkPartOrderResponse workPartOrderResponse = workPartOrderResponseList.get(position);
                Intent mIntent = new Intent(ProduceTeamOrderListActivity.this, ProduceTeamOrderDetailFragmentActivity.class);
                mIntent.putExtra(ConstantUtil.KEY_1, workPartOrderResponse);
                startActivityForResult(mIntent,ConstantUtil.START_ACTIVITY_KEY_1);
            }
        });
    }

    @Override
    public void setListener() {
        rl_back.setOnClickListener(this);
    }

    @Override
    public void widgetClick(View view) {
        switch (view.getId()) {
            case R.id.rl_back:
                finish();
                break;
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (null != data) {
            switch (requestCode) {
                case ConstantUtil.START_ACTIVITY_KEY_1:
                    getProductWorkPartAssignDoneOrderListPresenter.getProductWorkPartAssignDoneOrderList();
                    break;
            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void getGetProductWorkPartAssignDoneOrderListSuccessed(List<WorkPartOrderResponse> workPartOrderResponseLs) {
        if (null != workPartOrderResponseLs && workPartOrderResponseLs.size() > 0) {
            workPartOrderResponseList.clear();
            workPartOrderResponseList.addAll(workPartOrderResponseLs);
            adapter.setData(workPartOrderResponseList);
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public void getGetProductWorkPartAssignDoneOrderListFailed(final String desc) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                showToast(desc);
            }
        });
    }

    @Override
    public void showLoading() {
        rl_progressbar.setVisibility(View.VISIBLE);
    }

    @Override
    public void hideLoading() {
        rl_progressbar.setVisibility(View.GONE);
    }
}
